package testing;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class PostTest {
	private static String postXml(String target, String content)
			throws Exception {
		System.out.println("About to post\nURL: " + target + "content: "
				+ content);
		String response = "";

		URL url = new URL(target);
		Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(
				"gatekeeper.mitre.org", 80));
		HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
		conn.setRequestMethod("POST");

		// Set connection parameters.
		conn.setDoInput(true);
		conn.setDoOutput(true);
		conn.setUseCaches(false);
		// Make server believe we are form data...
		conn.setRequestProperty("Content-Type", "text/xml");
		DataOutputStream out = new DataOutputStream(conn.getOutputStream());
		// Write out the bytes of the content string to the stream.
		out.writeBytes(content);
		out.flush();
		out.close();
		// Read response from the input stream.
		BufferedReader in = new BufferedReader(new InputStreamReader(
				conn.getInputStream()));
		String temp;
		while ((temp = in.readLine()) != null) {
			response += temp + "\n";
		}
		temp = null;
		in.close();
		System.out.println("Server response:\n'" + response + "'");
		return response;
	}

	public static void main(String[] args) {

		try {
			String xml = "<gestures><gesture type=\"gestures.tapGestures.TapGesture\" time=\"12/31/2009 11:59:59\">"
					+ "<coordinates><coordinate x=\"50.532\" y=\"12\"/><coordinate x=\"10\" y=\"15\"/></coordinates><prevCoordinates></prevCoordinates>"
					+ "</gesture><gesture index=\"1\" type=\"gestures.slideGestures.SlidingGesture\" time=\"12/31/2010 23:59:59\"><coordinates>"
					+ "<coordinate x=\"50.532\" y=\"12\"/><coordinate x=\"10\" y=\"15\"/></coordinates><prevGestures>"
					+ "<prevGesture type=\"gestures.slideGestures.SlideGestureStart\" time=\"12/31/2010 23:59:50\">"
					+ "<coordinate x=\"50.532\" y=\"12\"/><coordinate x=\"80.532\" y=\"19\"/><coordinate x=\"20.532\" y=\"90\"/><coordinate x=\"90.532\" y=\"15\"/>"
					+ "</prevGesture><prevGesture type=\"gestures.slideGestures.SlidingGesture\" time=\"12/31/2010 23:59:55\"><coordinate x=\"10\" y=\"15\"/>"
					+ "</prevGesture></prevGestures></gesture></gestures>";

			String x = postXml("http://MM178187-PC.mitre.org:8080", xml);
			System.out.println(x);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
